package com.accesshollywood.indexables;

import java.util.Collections;
import java.util.Comparator;

import com.accesshollywood.components.Permissable;
import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.grfxOrdering.panels.ViewFiltersMasterPanel;
import com.accesshollywood.viewFiltering.ViewFilter;

public class ViewFilterPreset extends Indexed implements Permissable {
	protected User				owner			= Users.currentUser;
	protected ViewFilterMap	viewFilterList	= new ViewFilterMap();
	// protected int idViewFilterList = -1;
	protected String			name			= "New Preset...";

	// protected LinkedHashMap<Integer, ViewFilter> checkedFilters = new LinkedHashMap<Integer, ViewFilter>();
	// protected LinkedHashSet<ViewFilter> set = new LinkedHashSet<ViewFilter>();

	@Override
	public void initSQL() {
		setThat(this);
		tableName = "viewPreset";
		idColumnName = "idViewPreset";
		elementColumnName.put(getField("owner"), "idOwner");
		// elementColumnName.put(getField("idViewFilterList"), "idViewFilterList");
		elementColumnName.put(getField("name"), "name");
		elementColumnName.put(getField("viewFilterList"), "idViewFilterList");
	}

	public int toSQL() {
		Collections.sort(ViewFiltersMasterPanel.selectedFilters, new Comparator() {
			public int compare(Object o1, Object o2) {
				ViewFilter v1 = (ViewFilter) o1;
				ViewFilter v2 = (ViewFilter) o2;
				return v1.getGroupName().compareToIgnoreCase(v2.getGroupName());
			}
		});
		if (ViewFiltersMasterPanel.selectedFilters.size() > 0) {
			for (int i = 0; i < ViewFiltersMasterPanel.selectedFilters.size(); i++) {
				ViewFilter currFilter = (ViewFilter) ViewFiltersMasterPanel.selectedFilters.get(i);
				// newPreset.getCheckedFilters().put(currFilter.hashCode(), currFilter);
				getViewFilterList().put(currFilter.hashCode(), currFilter);
			}
		}
		return super.toSQL();
	}

	@Override
	public boolean isCurrentUserPermitted() {
		return (Users.currentUser.isAdmin() || Users.currentUser == getOwner());
	}

	public String toString() {
		return name;
	}

	public void fromString(String str) {
		this.name = str;
	}

	public synchronized User getOwner() {
		return owner;
	}

	public synchronized void setOwner(User owner) {
		owner = owner;
	}

	// public synchronized int getIdViewFilterList() {
	// return idViewFilterList;
	// }

	// public synchronized void setIdViewFilterList(int idViewFilterList) {
	// this.idViewFilterList = idViewFilterList;
	// }

	public synchronized ViewFilterMap getViewFilterList() {
		return viewFilterList;
	}

	public synchronized void setViewFilterList(ViewFilterMap viewFilterList) {
		this.viewFilterList = viewFilterList;
	}

	public synchronized String getName() {
		return name;
	}

	public synchronized void setName(String name) {
		this.name = name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((owner == null) ? 0 : owner.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		ViewFilterPreset other = (ViewFilterPreset) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (owner == null) {
			if (other.owner != null)
				return false;
		} else if (!owner.equals(other.owner))
			return false;
		return true;
	}

	/*
	 * public int idViewPreset = -1;
	 * public User owner;
	 * public LinkedHashMap<Integer, ViewFilter> checkedFilters;
	 * public String name;
	 * public ViewFilterPreset() {
	 * checkedFilters = new LinkedHashMap<Integer, ViewFilter>();
	 * }
	 * public int toSQL() {
	 * int idViewFilterList = GFXSQL.getNextAvailableID("ViewFilterList");
	 * if (checkedFilters.size() > 0) {
	 * for (ViewFilter currFilter : checkedFilters.values()) {
	 * GFXSQL.addInts("ViewFilterList", new Object[] { "idViewFilterList", "idViewFilter" }, new Object[] { idViewFilterList, currFilter.hashCode() });
	 * }
	 * } else {
	 * GFXSQL.addInts("ViewFilterList", new Object[] { "idViewFilterList", "idViewFilter" }, new Object[] { idViewFilterList, 0 });
	 * }
	 * int idViewPreset = GFXSQL.addInts("viewPreset", new Object[] { "idViewFilterList", "idPerson" }, new Object[] { idViewFilterList, owner.idPerson });
	 * GFXSQL.alterString("viewPreset", "name", name, idViewPreset);
	 * return idViewPreset;
	 * }
	 * @Override
	 * public void setValue(Object newValue, boolean toSQL) {
	 * if (newValue instanceof String) {
	 * name = (String) newValue;
	 * } else {
	 * name = ((ViewFilterPreset) newValue).name;
	 * }
	 * if (toSQL) {
	 * if (idViewPreset != -1) {
	 * GFXSQL.alterString("viewPreset", "name", name, idViewPreset);
	 * }
	 * }
	 * }
	 * @Override
	 * public Object getValue(boolean fromSQL) {
	 * if (fromSQL) {
	 * name = GFXSQL.getString("viewPreset", "name", "idViewPreset=" + idViewPreset)[0];
	 * }
	 * return name;
	 * }
	 * @Override
	 * public boolean isCurrentUserPermitted() {
	 * if (Users.currentUser == owner || Users.currentUser.isAdmin) {
	 * return true;
	 * } else {
	 * return false;
	 * }
	 * }
	 * @Override
	 * public boolean isEmpty() {
	 * // TODO Auto-generated method stub
	 * return false;
	 * }
	 */
}
